import moment from "moment";

/**
 * 转大写中文（转换一次）
 * 原文链接：https://segmentfault.com/a/1190000008962568
 */
export function numberConvertToUppercase(str) {
    if (str.indexOf('10') > -1) {
        return str.replace('10', '十');
    }
    let _str = '';
    let upperCaseNumber = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
    str = str.split('');
    for (let i in str) {
        let item = str[i];
        let num = Number(item);
        if (isNaN(num)) {
            _str += `${item}`;
            continue;
        }
        _str += `${upperCaseNumber[num]}`;
    }
    return _str;
}


export function randomString(len) {
    len = len || 32;
    let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
    let maxPos = $chars.length;
    let pwd = '';
    for (let i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}


export function defaultValue(value, defaultValue) {
    if (isUndefined(value) || isNull(value)) {
        return defaultValue
    }
    return value;
}

export function isNull(value) {
    return toString.call(value, String) === "[object Null]";
}

export function isEmpty(value) {
    if (isUndefined(value) || isNull(value)) {
        return true;
    }
    if ((isString(value) || isArray(value)) && value.length === 0) {

        return true;
    }
    if (isObject(value) && Object.values(value).length === 0) {
        return true;
    }
    return false;
}

export function isObject(value) {
    return toString.call(value, String) === "[object Object]";
}


export function isArray(value) {
    return toString.call(value, String) === "[object Array]";
}


export function isString(value) {
    return typeof value === 'string';
}

export function isUndefined(value) {
    return typeof value === 'undefined';
}

export function isTrue(value) {
    if (isBoolean(value)) {
        return value;
    } else if (isNumeric(value)) {
        return value != 0;
    } else if (isString(value)) {
        return value === 'true';
    }
    return false;
}

export function isBoolean(value) {
    return typeof value === 'boolean';
}

export function isFunction(value) {
    return typeof value === 'function';
}

export function isNumber(value) {
    return !isNaN(value) && typeof value === 'number';
}

export function isNumeric(value) {
    return isNumber(Number(value));
}

export function isInteger(value, isStrict = true) {
    if (isStrict) {
        return Number.isInteger(value);
    }
    return Number.isInteger(Number(value));
}

/**
 * 加法
 * @param arg1
 * @param arg2
 * @returns {number}
 */
export function bcAdd(arg1, arg2) {
    let r1, r2, m;
    try {
        r1 = arg1.toString().split('.')[1].length;
    } catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split('.')[1].length;
    } catch (e) {
        r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    return (arg1 * m + arg2 * m) / m;
}

/**
 * 乘法
 * @param arg1
 * @param arg2
 * @returns {number}
 */
export function bcMul(arg1, arg2) {
    let m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try {
        m += s1.split(".")[1].length
    } catch (e) {
    }
    try {
        m += s2.split(".")[1].length
    } catch (e) {
    }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}

/**
 * 除法
 * @param arg1
 * @param arg2
 * @returns {number}
 */
export function bcDiv(arg1, arg2) {
    let t1 = 0, t2 = 0, r1, r2;
    try {
        t1 = arg1.toString().split(".")[1].length;
    } catch (e) {
    }
    try {
        t2 = arg2.toString().split(".")[1].length;
    } catch (e) {
    }
    r1 = Number(arg1.toString().replace(".", ""));
    r2 = Number(arg2.toString().replace(".", ""));
    return (r1 / r2) * Math.pow(10, t2 - t1);
}

export function bcSub(arg1, arg2) {
    let r1, r2, max, min;
    try {
        r1 = arg1.toString().split(".")[1].length
    } catch (e) {
        r1 = 0
    }
    try {
        r2 = arg2.toString().split(".")[1].length
    } catch (e) {
        r2 = 0
    }
    max = Math.pow(10, Math.max(r1, r2));
    //动态控制精度长度
    min = (r1 >= r2) ? r1 : r2;
    return ((arg1 * max - arg2 * max) / max).toFixed(min)
}

export const bcComp = (a, b) => {
    a = Number(a);
    b = Number(b);
    if (isNaN(a)) {
        a = 0;
    }
    if (isNaN(b)) {
        b = 0;
    }
    if (a === b) {
        return 0;
    }
    return a > b ? 1 : -1;
};

export function formatMoney(val, v2 = 2) {
    val = Number(val);
    if (isNaN(val)) {
        return 0;
    }
    return Number(val.toFixed(v2));
}


export function dateFormat(time, format = 'Y-m-d H:i:s') {
    format = format.replace('Y', 'YYYY').replace('y', 'YYYY')
        .replace('m', 'MM').replace('d', 'DD')
        .replace('H', 'HH').replace('i', 'mm')
        .replace('s', 'ss');
    time = time || new Date();
    return moment(time).format(format);

}


